package com.example.photobrowser;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.media.ExifInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;


public class MainActivity extends Activity {
	
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			
			GridView gridview = (GridView) findViewById(R.id.gridview);
		    gridview.setAdapter(new ImageAdapter(this));
		    
		    gridview.setOnItemClickListener(new OnItemClickListener() {
		    	@Override
		    	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		    		Log.d("gridView","position [" + position + "]");
//		            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
		    		Intent i = new Intent(MainActivity.this, PictureView.class);
		    		i.putExtra("ID", position);
            	    startActivity(i);
		        }
		    });	
		}	  
	
	    public void einToast() {
	        Toast toast = Toast.makeText(this, "Ich bin nur ein kleiner Toast... :) ", Toast.LENGTH_SHORT);
	        toast.show();
	    }
	    
	    public static StringBuilder getExifStringOutput(String path){
	    	StringBuilder builder = new StringBuilder();
	    	ExifInterface exif;
			try {
				exif = new ExifInterface(path);						
            
	            builder.append("Date & Time: " + getExifTag(exif,ExifInterface.TAG_DATETIME) + "\n\n");
	            builder.append("Flash: " + getExifTag(exif,ExifInterface.TAG_FLASH) + "\n");
	            builder.append("Focal Length: " + getExifTag(exif,ExifInterface.TAG_FOCAL_LENGTH) + "\n\n");
	            builder.append("GPS Datestamp: " + getExifTag(exif,ExifInterface.TAG_FLASH) + "\n");
	            builder.append("GPS Latitude: " + getExifTag(exif,ExifInterface.TAG_GPS_LATITUDE) + "\n");
	            builder.append("GPS Latitude Ref: " + getExifTag(exif,ExifInterface.TAG_GPS_LATITUDE_REF) + "\n");
	            builder.append("GPS Longitude: " + getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE) + "\n");
	            builder.append("GPS Longitude Ref: " + getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE_REF) + "\n");
	            builder.append("GPS Processing Method: " + getExifTag(exif,ExifInterface.TAG_GPS_PROCESSING_METHOD) + "\n");
	            builder.append("GPS Timestamp: " + getExifTag(exif,ExifInterface.TAG_GPS_TIMESTAMP) + "\n\n");
	            builder.append("Image Length: " + getExifTag(exif,ExifInterface.TAG_IMAGE_LENGTH) + "\n");
	            builder.append("Image Width: " + getExifTag(exif,ExifInterface.TAG_IMAGE_WIDTH) + "\n\n");
	            builder.append("Camera Make: " + getExifTag(exif,ExifInterface.TAG_MAKE) + "\n");
	            builder.append("Camera Model: " + getExifTag(exif,ExifInterface.TAG_MODEL) + "\n");
	            builder.append("Camera Orientation: " + getExifTag(exif,ExifInterface.TAG_ORIENTATION) + "\n");
	            builder.append("Camera White Balance: " + getExifTag(exif,ExifInterface.TAG_WHITE_BALANCE) + "\n");
	            builder.append("ISO: " + getExifTag(exif,ExifInterface.TAG_ISO) + "\n");
	            builder.append("Exposure Time: " + getExifTag(exif,ExifInterface.TAG_EXPOSURE_TIME) + "\n");
				} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			
	    	return builder;
	    }
	    
	    public static String getExifTag(ExifInterface exif,String tag){
            String attribute = exif.getAttribute(tag);
            
            return (null != attribute ? attribute : "");
	    }
	
	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        // Inflate the menu; this adds items to the action bar if it is present.
	        getMenuInflater().inflate(R.menu.actionbar, menu);
	        return true;
	    }	
	    
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	            case android.R.id.home:
	                // app icon in action bar clicked; go home
	                Intent intent = new Intent(this, MainActivity.class);
	                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	                startActivity(intent);
	                return true;
	            case R.id.menu_add:
//	            	einToast();
	            	Toast.makeText(this, R.drawable.amsterdam, Toast.LENGTH_SHORT).show();
//            		Intent intent2 = new Intent(this, Test.class);
//            	    startActivity(intent2);
	            	return true;
	            case R.id.menu_search:
//	            	einToast();
					StringBuilder builder = getExifStringOutput(getString(R.drawable.amsterdam));
	     	        Toast.makeText(this, builder.toString(), Toast.LENGTH_SHORT).show();
	     	        builder=null;
//	            	invalidateOptionsMenu();
	            	return true;
	            case R.id.menu_filter:
//	            	einToast();
	            	Intent filIntent = new Intent(MainActivity.this, FilterActivity.class);	                
	                startActivity(filIntent);
	            	return true;
	            case R.id.menu_pref:
	            	einToast();
	            	return true;
	            default:
	                return super.onOptionsItemSelected(item);
	        }
	    }
	    
//	   @Override
//	   public boolean onPrepareOptionsMenu(Menu menu){
//		   getMenuInflater().inflate(R.menu.actionbar, menu);
//		   return true;	    	
//	   }

}
